package com.pmsystem.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pmsystem.dao.VillageMapper;
import com.pmsystem.pojo.Building;
import com.pmsystem.pojo.BuildingShow;
import com.pmsystem.pojo.Room;
import com.pmsystem.pojo.RoomKey;
import com.pmsystem.pojo.RoomShow;
import com.pmsystem.pojo.Village;


/**
 * 
* <p>Title: BatchService</p>  
* <p>Description:批量操作的业务层 </p>  
* @author 树昌  
* @date 2019年1月26日
 */
@Service
public class BatchService {

	@Autowired
	VillageService villageService;
	@Autowired
	BuildingService buildingService;
	@Autowired
	RoomService roomService;
	
	
	@Autowired
	RoomKeyService roomKeyService;
	
	/**
	 * 
	 * <p>Title: batchAddVBR</p>  
	 * <p>Description:批量添加小区，大楼，房间信息 </p>  
	 * @author 树昌
	 * @param villagename	小区名称的集合
	 * @param buildingnum	每个小区的大楼数量
	 * @param floornum		大楼的楼层数
	 * @param roomnum		每层楼的房间数量
	 * @return
	 */
	public boolean	batchAddVBR(List<String> villagename,int buildingnum,int floornum,int roomnum) {
		
		int villagelength = villagename.size();
		if((villagelength>20||villagelength<=0)||(buildingnum<0||buildingnum>20)||(floornum<0||floornum>30)||(roomnum<=0||roomnum>20)) {
			return false;
		}
		
		
		//批量操作之前先清空数据库表中的内容
		villageService.deleteVillageTableData();
		buildingService.deleteBuildingTableData();
		roomService.deleteRoomTableData();
		
		//按照传入的字符串创建小区信息
		for(int i=0;i<villagelength;i++) {
			Village village = new Village();
			village.setVillageName(villagename.get(i));
			villageService.addVillage(village);
		}
		
		//将所有小区信息查询出来
		List<Village> listvillage = villageService.showVillage(null, null, null);
		if(listvillage==null) {
			return false;
		}
		
		//根据小区的记录条数创建大楼信息
		Building building = new Building();
		int alphabet = 65;
		for (Village village : listvillage) {
			for(int i=0;i<buildingnum;i++) {
				building.setBuildingName(((char)(alphabet+i))+"");
				building.setBlgVillageId(village.getVillageId());
				
				
				boolean row = buildingService.addBuilding(building);
				if(!row) {
					villageService.deleteVillageTableData();
					buildingService.deleteBuildingTableData();
					roomService.deleteRoomTableData();
					return false;
				}
			}
		}
		
		//查询所有大楼的信息
		List<BuildingShow> listbuilding = buildingService.showBuilding(null, null, null,null);
		if(listbuilding==null) {
			return false;
		}
		
		//根据大楼的信息条数创建房间信息
		Room room = new Room();
		for (BuildingShow building2 : listbuilding) {
			
			for(int i=1;i<=floornum;i++) {
				for(int j=1;j<=roomnum;j++) {
					room.setBlgBuildingId(building2.getBuildingId());
					room.setFloor(i);
					if(j<10) {
						room.setRoomName((i+"")+("0"+j));
					}else {
						room.setRoomName((i+"")+(j+""));
					}
					
					boolean row = roomService.addRoom(room);
					if(!row) {
						villageService.deleteVillageTableData();
						buildingService.deleteBuildingTableData();
						roomService.deleteRoomTableData();
						return false;
					}
				}
			}
		}

		return true;	
	}
	
	
	public boolean batchAddRoomKey(int keynum) {
		
		// 批量添加前，先清空数据表
		roomKeyService.deleteRoomKeyTableData();

		List<RoomShow> listroom = roomService.showRoom(null, null, null,null,null);

		RoomKey roomKey = new RoomKey();
		for (RoomShow room : listroom) {

			roomKey.setBlgRoomId(room.getRoomId());
			roomKey.setIsLend((byte) 0);

			for (int i = 0; i < keynum; i++) {
				boolean row = roomKeyService.addRoomkey(roomKey);
				if (!row) {
					roomKeyService.deleteRoomKeyTableData();
					return false;
				}
			}
		}

		return true;
	}
	
	
}
